#include "mainwindow.h"
#include "ui_SERVER.h"
#include <QSqlDatabase>
#include <QSqlError>
#include <QSqlQuery>
#include <QDebug>
#include <QString>
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    //链接查询按钮到查询事件
    connect(ui->SearchBtn,SIGNAL(clicked()),
            this,SLOT(SelectSlot()));
    //链接到数据库，数据库环境sqlite3
    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName("my.db");
    bool ok = db.open();
    if (!ok) {
        qDebug() << "Error: Could not connect to database." << db.lastError();
    }else{
        qDebug() << "connect to database.";
    }

}

void MainWindow::SelectSlot()
{
    //清空查询结果显示区
    ui->text_browser->clear();
    //操作数据库
    QSqlQuery sqlQuery;
    QString Word = ui->WordInput->text();
    QString str_sql = "";
    str_sql = QString("select * from dic where word like '%1%'limit 10").arg(Word);
    if(sqlQuery.exec(str_sql))
    {
        qDebug()<<"查询dic表成功";
        while(sqlQuery.next())
        {
            qDebug()<<"成功进入查询结果"<<endl;
            QString word = sqlQuery.value("word").toString();
            QString parse = sqlQuery.value("parse").toString();
            qDebug()<<word<<endl<<parse<<endl;
            ui->text_browser->append(word);
            ui->text_browser->append(parse);
        }
    }
    else
    {
        qDebug()<<"查询dic表失败！！";

    }
}

MainWindow::~MainWindow()
{
    delete ui;
}
